package com.hyh.spring.service.impl;

import com.hyh.spring.service.IMessageProvider;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.messaging.Source;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.UUID;

/**
 * @BelongsPackage: com.hyh.spring.service.impl
 * @ClassName IMessageProviderimpl
 * @Author: hyhWTX
 * @CreateTime: 2023-05-03  16:36
 * @Description: 发送消息接口实现类
 * @Version: 1.0
 */
//@Service此处不需要写，不同于传统的AOP方式，此处我们使用的是是与rabbitmq进行交流的，使用 @EnableBinding
//@EnableBinding：指信道channel和exchange绑定在一起
@EnableBinding(Source.class)
//可以理解为定义消息的发送管道Source对应output(生产者)，Sink对应input(消费者)
//引入Source，定义消息的推送管道
@Slf4j
public class IMessageProviderimpl implements IMessageProvider {

    @Resource
    private MessageChannel output;// 消息的发送管道，MessageChannel对象的实例名必须是output
    @Override
    public String sendMessage() {
        String serial = UUID.randomUUID().toString();
        //创建消息，通过output这个管道向消息中间件发消息
        output.send(MessageBuilder.withPayload(serial).build());
        log.info("********serial："+serial);
        return null;
    }
}
